﻿
using System;
using System.Threading.Tasks;

namespace Magic.Redis;


/// <summary>
/// 提供Redis客户端操作类（基于CsRedisCore实现）
/// </summary>
public sealed partial class CsRedisCoreClient : IRedisClient
{
	/// <summary>
	/// ListGetByIndex.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="index"></param>
	/// <returns></returns>
	public T ListGetByIndex<T>(string key, long index)
	{
		try
		{
			return RedisHelper.LIndex<T>(key, index);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListGetByIndex.key={key}&index={index}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListGetByIndexAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="index"></param>
	/// <returns></returns>
	public Task<T> ListGetByIndexAsync<T>(string key, long index)
	{
		try
		{
			return RedisHelper.LIndexAsync<T>(key, index);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListGetByIndexAsync.key={key}&index={index}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListLeftPop.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <returns></returns>
	public T ListLeftPop<T>(string key)
	{
		try
		{
			return RedisHelper.LPop<T>(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListLeftPop.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListLeftPopAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <returns></returns>
	public Task<T> ListLeftPopAsync<T>(string key)
	{
		try
		{
			return RedisHelper.LPopAsync<T>(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListLeftPopAsync.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListLeftPush.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public long ListLeftPush<T>(string key, T value)
	{
		try
		{
			return RedisHelper.LPush(key, value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListLeftPush.key={key}&value={Serialize(value)}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListLeftPushAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public Task<long> ListLeftPushAsync<T>(string key, T value)
	{
		try
		{
			return RedisHelper.LPushAsync(key, value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListLeftPushAsync.key={key}&value={Serialize(value)}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListLength.
	/// </summary>
	/// <param name="key"></param>
	/// <returns></returns>
	public long ListLength(string key)
	{
		try
		{
			return RedisHelper.LLen(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListLength.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListLengthAsync.
	/// </summary>
	/// <param name="key"></param>
	/// <returns></returns>
	public Task<long> ListLengthAsync(string key)
	{
		try
		{
			return RedisHelper.LLenAsync(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListLengthAsync.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListRightPop.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <returns></returns>
	public T ListRightPop<T>(string key)
	{
		try
		{
			return RedisHelper.RPop<T>(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListRightPop.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListRightPopAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <returns></returns>
	public Task<T> ListRightPopAsync<T>(string key)
	{
		try
		{
			return RedisHelper.RPopAsync<T>(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListRightPopAsync.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListRightPush.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public long ListRightPush<T>(string key, T value)
	{
		try
		{
			return RedisHelper.RPush(key, value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListRightPush.key={key}&value={Serialize(value)}", ex);
			return default;
		}
	}

	/// <summary>
	/// ListRightPushAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public Task<long> ListRightPushAsync<T>(string key, T value)
	{
		try
		{
			return RedisHelper.RPushAsync(key, value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on ListRightPushAsync.key={key}&value={Serialize(value)}", ex);
			return default;
		}
	}
}
